package com.test.burnincash.util_burnin.helper_burnin;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.test.burnincash.util_burnin.StringUtil_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseApplication_burnin;

/**
 * 用户信息管理
 * 初始化本地token/mobile等数据
 */
public class UserInfoHelper_burnin {

    public static final String SP_KEY_TOKEN = "token";
    public static final String SP_KEY_MOBILE = "phoneNum";
    public static final String SP_KEY_USERID = "userid";
    public static final String SP_KEY_IS_AD_SOURCE = "isAdSource";

    public static final String LOGIN_TYPE_REGISTER = "REGISTER";
    public static final String LOGIN_TYPE_OLD = "OLD";

    /**
     * 用户是否选择过额度
     */
    public static String USER_HAD_SELECTED_AMOUNT_FLAG = "NO";

    /**
     * 全局sp
     */
    private final SharedPreferences mSharedPreferences;

    /**
     * 登录手机号
     */
    private String mMobile;
    /**
     * 登录token
     */
    private String mToken;
    /**
     * 登录user_id
     */
    private String mUserId;
    /**
     * 是否为渠道用户，是的话，走贷款流程，不是的话，走壳子包
     */
    private String mIsAdSource;

    private static class InnerUserInfoHelper {
        private static final UserInfoHelper_burnin instance = new UserInfoHelper_burnin();
    }

    public static UserInfoHelper_burnin getInstance() {
        return InnerUserInfoHelper.instance;
    }

    private UserInfoHelper_burnin() {
        mSharedPreferences = BaseApplication_burnin.getApplication_burnin().getSharedPreferences(DeviceHelper_burnin.getPackageName(), Context.MODE_PRIVATE);
        this.mMobile = mSharedPreferences.getString(SP_KEY_MOBILE, "");
        this.mIsAdSource = mSharedPreferences.getString(SP_KEY_IS_AD_SOURCE, "");
        this.mToken = mSharedPreferences.getString(SP_KEY_TOKEN, "");
        this.mUserId = mSharedPreferences.getString(SP_KEY_USERID, "");
    }

    public void init(Context context) {}

    //判断是否已经登录
    public boolean didLogin() {
        return (!TextUtils.isEmpty(this.mToken));
    }

    public String getMobile() {
        mMobile = StringUtil_burnin.getSafeString(mMobile);
        return mMobile;
    }

    public String getHideMobile() {
        return StringUtil_burnin.bankAccountHideFront(getMobile());
    }

    @SuppressLint("ApplySharedPref")
    public void setMobile(String mobile) {
        this.mMobile = StringUtil_burnin.getSafeString(mobile);
        mSharedPreferences.edit().putString(SP_KEY_MOBILE, this.mMobile).commit();
    }

    public String getToken() {
        mToken = StringUtil_burnin.getSafeString(mToken);
        return mToken;
    }

    public String getUserId() {
        mUserId = StringUtil_burnin.getSafeString(mUserId);
        return mUserId;
    }

    @SuppressLint("ApplySharedPref")
    public void setToken(String token) {
        this.mToken = StringUtil_burnin.getSafeString(token);
        mSharedPreferences.edit().putString(SP_KEY_TOKEN, this.mToken).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void setUserId(String userId) {
        this.mUserId = StringUtil_burnin.getSafeString(userId);
        mSharedPreferences.edit().putString(SP_KEY_USERID, this.mUserId).commit();
    }

    public String getIsAdSourceString() {
        return StringUtil_burnin.getSafeString(mIsAdSource);
    }

    public boolean isAdSource() {
        return StringUtil_burnin.getSafeString(mIsAdSource).equalsIgnoreCase("YES");
    }

    @SuppressLint("ApplySharedPref")
    public void setIsAdSource(String isAdSource) {
        this.mIsAdSource = StringUtil_burnin.getSafeString(isAdSource);
        mSharedPreferences.edit().putString(SP_KEY_IS_AD_SOURCE, this.mIsAdSource).commit();
    }

}
